<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="项目名称" prop="pics">
        <el-input v-model="queryParams.name" placeholder="请输入" clearable class="!w-240px" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入" clearable class="!w-240px" />
      </el-form-item>
      <el-form-item label="总包公司">
        <el-select placeholder="请选择总包公司" v-model="queryParams.company" clearable>
          <el-option v-for="(item) in userList" :key="item.id" :label="item.nickname" :value="item.nickname" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" />重置</el-button>
        <el-button @click="handleQuery" style="margin-right:10px;"><Icon icon="ep:search" />搜索</el-button>

      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-form>
      <el-form-item style="display: flex; flex-direction: column;">
        <el-button style="margin-left: 20px;" type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['system:project:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="primary" plain @click="openForm()">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list">
      <el-table-column type="selection" width="55" />
      <!-- <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="照片" align="center" prop="pics" /> -->
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="项目经理电话" align="center" prop="userId" />
      <el-table-column label="总包公司" align="center" prop="company" />
      <!-- <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="area" /> -->
      <el-table-column label="项目地址" align="center" prop="address" />
      <!-- 
      <el-table-column label="经度" align="center" prop="lon" />
      <el-table-column label="维度" align="center" prop="lat" /> -->
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="edit(scope.row.id)" v-hasPermi="['system:project:update']">
            编辑
          </el-button>
           <el-button link type="primary" @click="handleDelete(scope.row.id)" v-hasPermi="['system:project:delete']">
            删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <!-- <ProjectForm ref="formRef" @success="getList" /> -->
</template>

<script setup lang="ts" name="Project">
import * as UserApi from '@/api/system/user'
import { useRouter } from "vue-router";
import download from '@/utils/download'
import * as ProjectApi from '@/api/system/project'
// import ProjectForm from './ProjectForm.vue'
const message = useMessage() // 消息弹窗
//const { t } = useI18n() // 国际化
const userList = ref([])
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  pics: null,
  company: null,
  province: null,
  city: null,
  area: null,
  address: null,
  lon: null,
  lat: null,
  startTime: [],
  endTime: [],
  userId: null,
  financeId: null,
  orderNumber: null,
  createTime: [],
  region: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectApi.getProjectPage(queryParams)
    list.value = data.list.map((item)=>{
      item.startTime = dateChangeFn(item.startTime)
      item.endTime = dateChangeFn(item.endTime)
      return{
        ...item
      }
    })
    total.value = data.total
    console.log(data, "shju------------------------------")
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.company = null
  handleQuery()
}
console.log(resetQuery)
/** 添加/修改操作 */
// const formRef = ref()

// 测试路由跳转
const router = useRouter()

// 跳转页面
const openForm = () => {
  router.push('/system/project/projectAdd')
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProjectApi.deleteProject(id)
    message.success('删除成功！')
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProjectApi.exportProject(queryParams)
    download.excel(data, '项目管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }                    
}              
const edit = (ids) => {                         
  router.push({ path: '/system/project/ProjectMain', query: { id: ids } })
}

const dateChangeFn=(times)=>{
  if(!times){
    return '暂无'
  }
  var date = new Date(times);  // 参数需要毫秒数，所以这里将秒数乘于 1000
  let Y = date.getFullYear() + '-';
   let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  let D = date.getDate() + ' ';
  // let h = date.getHours() + ':';
  // let m = date.getMinutes() + ':';
  // let s = date.getSeconds();
  return Y+M+D
}
/** 初始化 **/
onMounted(async() => {
  getList()
  // 加载用户列表
  userList.value = await UserApi.getSimpleUserAllList()
  console.log(userList.value,'userList.value')
})
</script>


<style lang="scss" scoped>
::v-deep.el-form-item--default .el-form-item__content {
  display: flex;
  flex-direction: row-reverse
}
</style>